﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Collections;
using System.Xml;
using System.IO;
using System.Text;

namespace Taxes.KML
{
    public class KMLOperate
    {
        private readonly string _fileName;
        public KMLOperate(string fileName)
        {
            _fileName = fileName;
        }

        public List<Region> GetLandUseChargeRegion()
        {
 var document = new XmlDocument();
            document.Load(_fileName);
            var manager = new XmlNamespaceManager(document.NameTable);
            manager.AddNamespace("gx", "http://www.google.com/kml/ext/2.2");
            manager.AddNamespace("kml", "http://www.opengis.net/kml/2.2");

            var name = document.SelectNodes("//kml:Placemark//kml:name", manager);
            var des = document.SelectNodes("//kml:Placemark//kml:description", manager);
            var coordinates =
                document.SelectNodes(
                    "//kml:Placemark//kml:Polygon//kml:outerBoundaryIs//kml:LinearRing//kml:coordinates", manager);
            if (name == null || des == null || coordinates == null)
                return null;
            if ((name.Count != des.Count || name.Count != coordinates.Count))
            {
                return null;
            }
            var regions = new List<Region>(name.Count);
            for (var i = 0; i < name.Count; i++)
            {
                var region = new Region
                {
                    Name = name[i].InnerText,
                    Postion = coordinates[i].InnerText.ToString().Replace("\n", "").Replace("\t", "")
                };

                string s = des[i].InnerText.Replace("\n", "").Replace("\t", "");
                   s=s.Substring(0,s.LastIndexOf("元"));

                region.Cost= Convert.ToSingle( s.Substring(3));
                regions.Add(region);
            }
            return regions;
        }


        public List<Region> GetRegion()
        {
            var document = new XmlDocument();
            document.Load(_fileName);
            var manager = new XmlNamespaceManager(document.NameTable);
            manager.AddNamespace("gx", "http://www.google.com/kml/ext/2.2");
            manager.AddNamespace("kml", "http://www.opengis.net/kml/2.2");

            var name = document.SelectNodes("//kml:Placemark//kml:name", manager);
            var des = document.SelectNodes("//kml:Placemark//kml:description", manager);
            var coordinates =
                document.SelectNodes(
                    "//kml:Placemark//kml:Polygon//kml:outerBoundaryIs//kml:LinearRing//kml:coordinates", manager);
            if (name == null || des == null || coordinates == null)
                return null;
            if ((name.Count != des.Count || name.Count != coordinates.Count))
            {
                return null;
            }
            var regions = new List<Region>(name.Count);
            for (var i = 0; i < name.Count; i++)
            {
                var region = new Region
                {
                    Name = name[i].InnerText,
                    Cost =
                        Convert.ToSingle(des[i].InnerText.Substring(0,
                                                                    des[i].InnerText.LastIndexOf("元"))),
                    Postion = coordinates[i].InnerText.ToString().Replace("\n", "").Replace("\t", "")
                };
                regions.Add(region);
            }
            return regions;
        }
    }

    public class KMLExport
    {
        private readonly string _fileName;
        public KMLExport(string fileName)
        {
            _fileName = fileName;
        }
        public void Export()
        {
            if (File.Exists(_fileName))
            {
                File.Delete(_fileName);
            }
            var bll = new BLL.region_info();
            var modelList = bll.GetModelList("");
            {
                var writer = new XmlTextWriter(_fileName, Encoding.UTF8)
                {
                    Formatting = Formatting.Indented
                };

                writer.WriteStartDocument();
                writer.WriteStartElement("kml", "http://www.opengis.net/kml/2.2");
                writer.WriteAttributeString("xmlns", "gx", null, "http://www.google.com/kml/ext/2.2");
                writer.WriteAttributeString("xmlns","kml",null,"http://www.opengis.net/kml/2.2");
                writer.WriteAttributeString("xmlns","atom",null,"http://www.w3.org/2005/Atom");
                writer.WriteStartElement("Document");

                writer.WriteStartElement("name");
                writer.WriteString("我的位置.kml");
                writer.WriteEndElement();

                foreach(var r in modelList)
                {
                    writer.WriteStartElement("Placemark");
                        writer.WriteStartElement("name");
                    writer.WriteString(r.region_name);
                    writer.WriteEndElement();
                    writer.WriteStartElement("description");
                    writer.WriteString(r.region_unit_price+@"元/平方米");
                    writer.WriteEndElement();

                    writer.WriteStartElement("Polygon");
                    writer.WriteStartElement("tessellate");
                    writer.WriteString("1");
                    writer.WriteEndElement();


                    writer.WriteStartElement("outerBoundaryIs");
                    writer.WriteStartElement("LinearRing");
                        writer.WriteStartElement("coordinates");
                            writer.WriteString(r.region_value);
                        writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.WriteEndElement();

                    writer.WriteEndElement();

                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndDocument();

                writer.Close();
            }
        }
    }
}